﻿


///绑定下拉列表
$(document).ready(function () {
    //依赖于edit.common.js

    //教育经历
    fnDataGridDataBing("datagrid_edu", "/extendpage/data/jsondata.aspx?entity=t_yginfo_edu&order=&condition=eid='" + instance.id + "'")
    //工作背景
    fnDataGridDataBing("datagrid_work", "/extendpage/data/jsondata.aspx?entity=t_yginfo_workback&order=&condition=eid='" + instance.id + "'")
    //家庭成员
    fnDataGridDataBing("datagrid_family", "/extendpage/data/jsondata.aspx?entity=t_yginfo_family&order=&condition=eid='" + instance.id + "'")
    //合同
    fnDataGridDataBing("datagrid_contract", "/extendpage/data/jsondata.aspx?entity=t_yginfo_contract&order=&condition=eid='" + instance.id + "'")

    //绑定数据到表单
    fnSetBaseInfo('t_yginfo_cw', 'cw_info');
    fnSetBaseInfo('t_yginfo_hr', 'hr_info');
    fnSetBaseInfo('t_yginfo_bg', 'bg_info');
});

//绑定信息到指定表单
//e表名 e1指定表单id

var fnSetBaseInfo = function (e, e1) {
    //依赖于/scripts/common/jquery.data.js
    var data = fnGetSingleData(e, "eid='" + instance.id + "'");
    //反序列化实体对象到表单
    if (data != null && typeof (data) != "undefined" && data.length > 0) {
       
        if (e == "t_yginfo_cw") {
            $("#cid").val((data[0].id));
        }
        if (e == "t_yginfo_hr") {
            $("#rid").val((data[0].id));
        }

        if (e == "t_yginfo_bg") {
            $("#bid").val((data[0].id));
        }
        fnCustomeSetForm(e1, data[0]);
    }
}
//添加grid行 e:grid的id
function fnOpAdd(e) {
    var newRow = { "eid": instance.id };
    mini.get(e).addRow(newRow, 0);
}
//删除grid行 e:grid的id
function fnOpRemove(e) {
    var rows = mini.get(e).getSelecteds();
    if (rows.length > 0) {
        if (e == "datagrid_contract") {
            if (confirm("确定要删除吗？")) {
                mini.get(e).removeRows(rows, true);
            }
        } else {
            mini.get(e).removeRows(rows, true);
        }
    } else {
        alert("请选择删除项！！！");
    }
}

///grid反序列化到json -----e  grid的id
function getCommentJson(e) {
    var grid = mini.get(e);
    var data = grid.getChanges();
    return mini.encode(data);
}


function fnCustomSave() {
    var form = new mini.Form("form1");

    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    if (form_data.name == "") {
        alert("请输入员工姓名！");
        return;
    }
    if (form_data.dept_id == "") {
        alert("请选择部门！");
        return;
    }
  

    var _data = fnCustomGetData("cw_info");
    _data.eid = instance.id;
    var _data2 = fnCustomGetData("hr_info");
    _data2.eid = instance.id;
    var _data3 = fnCustomGetData("bg_info");
    _data3.eid = instance.id;

    form_data.brith_date = mini.formatDate(form_data.brith_date, "yyyy-MM-dd");//出生日期
    form_data.frist_join_date = mini.formatDate(form_data.frist_join_date, "yyyy-MM-dd");//参加工作日期
    form_data.join_job_date = mini.formatDate(form_data.join_job_date, "yyyy-MM-dd");//进本单位日期
    form_data.regularworker_time = mini.formatDate(form_data.regularworker_time, "yyyy-MM-dd");//转正日期
    form_data.leave_time = mini.formatDate(form_data.leave_time, "yyyy-MM-dd");//离职日期
    form_data.retire_time = mini.formatDate(form_data.retire_time, "yyyy-MM-dd");//退休日期



    fnClone(form_data, instance);

    if (typeof (instance) != "undefined" && instance != null) {
        //var msg = mini.loading("数据保存中，请稍后......", "系统消息提醒");
        var json = mini.encode(instance);   //序列化成JSON  基本信息
        var json1 = mini.encode(_data); //财务信息
        json1 = json1.substring(0, json1.length - 1) + ",\"id\":\"" + $("#cid").val() + "\"}";

        var json2 = mini.encode(_data2); //人力资源信息
        json2 = json2.substring(0, json2.length - 1) + ",\"id\":\"" + $("#rid").val() + "\"}";

        var json3 = mini.encode(_data3); //报关信息
        json3 = json3.substring(0, json3.length - 1) + ",\"id\":\"" + $("#bid").val() + "\"}";
        //教育背景
        var json4 = getCommentJson('datagrid_edu');
        //工作背景
        var json5 = getCommentJson('datagrid_work');
        //家庭成员
        var json6 = getCommentJson('datagrid_family');
        //合同
        var json7 = getCommentJson('datagrid_contract');
       
        json = json + "[@@@]" + json1 + "[@@@]" + json2 + "[@@@]" + json3 + "[@@@]" + json4 + "[@@@]" + json5 + "[@@@]" + json6 + "[@@@]" + json7;
        $.ajax({
            url: "/extendpage/action/datasave.aspx?entity=t_base_employee:t_yginfo_cw:t_yginfo_hr:t_yginfo_bg:t_yginfo_edu:t_yginfo_workback:t_yginfo_family:t_yginfo_contract&flag=1&action=mutibatchsave",
            type: "post",
            data: { data: json },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    alert("数据保存成功!")
                    fnClose();
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
        mini.hideMessageBox(msg);
    }
}